package com.orion.controller;

import com.orion.entity.Result;
import com.orion.enumeration.CommonCodeEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @date 2021/10/30
 */
@RestController
public class ProviderHystrixController {

    @Value("${testHystrix.sleep}")
    private Integer testHystrixSleep;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/providerHystrix")
    public Result providerHystrix() {
        String resultStr = "port : " + port + " --- testHystrixSleep : " + testHystrixSleep;
        System.out.println(System.currentTimeMillis() + " --" + resultStr);
        if (testHystrixSleep > 0) {
            try {
                TimeUnit.SECONDS.sleep(testHystrixSleep);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return CommonCodeEnum.COMMON_SUCCESS.toResult(resultStr);
    }

    @GetMapping("/providerPartHystrix")
    public Result providerPartHystrix(@RequestParam Integer a) {
        String resultStr = "port : " + port + " --- testPartHystrixSleep : " + testHystrixSleep + " -- a : " + a;
        System.out.println(System.currentTimeMillis() + " --" + resultStr);
        if (testHystrixSleep > 0) {
            try {
                TimeUnit.SECONDS.sleep(testHystrixSleep);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return CommonCodeEnum.COMMON_SUCCESS.toResult(resultStr);
    }

    @GetMapping("/providerPartCircuit")
    public Result providerPartCircuit(@RequestParam Integer a) {
        String resultStr = "port : " + port + " --- testPartCircuitSleep : " + testHystrixSleep + " -- a : " + a;
        System.out.println(System.currentTimeMillis() + " --" + resultStr);
        if (testHystrixSleep > 0) {
            try {
                TimeUnit.SECONDS.sleep(testHystrixSleep);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return CommonCodeEnum.COMMON_SUCCESS.toResult(resultStr);
    }

}
